package de.cbf.cam.dsa.model.entities;

import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import de.cbf.cam.dsa.model.entities.items.Item;
import de.cbf.cam.dsa.model.entities.items.ItemOnGround;
import de.cbf.cam.dsa.model.util.Position3f;

public class Ground {
	
	private static  Ground instance = new Ground();
	
	/**
	 * @uml.property  name="itemsOnGround"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.items.ItemOnGround"
	 */
	private List<ItemOnGround> itemsOnGround = new ArrayList<ItemOnGround>();
	
	
	public static final String PROPERTYNAME = "Ground:";
	/**
	 * @uml.property  name="propertyChangeSupport"
	 */
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	/**
	 * will return null if item is not found
	 * @param guid
	 * @return
	 */
	public Item getItemByGUID(String guid) {
		for(int i = 0;i<itemsOnGround.size();i++){
			Item item = itemsOnGround.get(i).getItem();
			if(item.getGuid().equals(guid)){
				return item;
			}
		}
		return null;
	}

	public static Ground getInstance() {		
		return instance;
	}
	
	public void addItemToGround(Item item, Position3f position){
		itemsOnGround.add(new ItemOnGround(item, position));
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"itemsOnGround",null,itemsOnGround);
	}
	
	public boolean removeItem(Item item) {
		for(int i = 0;i<itemsOnGround.size();i++){
			if(item.getGuid().equals(itemsOnGround.get(i).getItem().getGuid())){
				removeItemFromGround(itemsOnGround.get(i));
			}
		}
		return false;
	}
	
	public boolean removeItemFromGround(ItemOnGround item){		
		boolean success = itemsOnGround.remove(item);
		if(success){
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+"itemsOnGround",null,itemsOnGround);
		}		
		return success;
	}

	

}
